package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class ProjectStakeholderTypeService{

	@Autowired
	private ProjectStakeholderTypeDao projectStakeholderTypeDao;
	
	private QProjectStakeholderType $ = QProjectStakeholderType.projectStakeholderType;
	
	@Autowired
	public void setProjectStakeholderTypeDao(ProjectStakeholderTypeDao projectStakeholderTypeDao) {
		this.projectStakeholderTypeDao = projectStakeholderTypeDao;
	}
	
	public List<ProjectStakeholderType> findAll(){
		//BooleanBuilder condition = new BooleanBuilder();
		return ListUtil.toList(projectStakeholderTypeDao.findAll());
	}
	
	public ProjectStakeholderType findOne(Long id){
		return this.projectStakeholderTypeDao.findOne(id);
	}
				
	public List<ProjectStakeholderType> findAll(
	String name,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(StringUtil.isNotEmpty(name)){
		condition.and($.name.contains(name));
	}
	
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(projectStakeholderTypeDao.findAll(condition));
	}		

	public Page<ProjectStakeholderType> findAll(
			String name,
			String remark,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(StringUtil.isNotEmpty(name)){
				condition.and($.name.contains(name));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
	
			/*
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			*/
			
			
			Sort sortObject_1 = new Sort(Direction.DESC, "orderNumber");
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject_1);
			return projectStakeholderTypeDao.findAll(condition, pageRequest);
	}
	
	
	
	public ProjectStakeholderType getProjectStakeholderType(Long id){
		return projectStakeholderTypeDao.getOne(id);
	}
	
	public void deleteProjectStakeholderType(Long id){
		projectStakeholderTypeDao.delete(id);
	}

	public void saveProjectStakeholderType(ProjectStakeholderType projectStakeholderType) {
		if (null == projectStakeholderType.getOrderNumber()) {
			projectStakeholderType.setOrderNumber(this.getOrderNumber());
		}
		projectStakeholderTypeDao.save(projectStakeholderType);
	}

	private int getOrderNumber(){
		BooleanBuilder condition = new BooleanBuilder();
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, 1, sortObject);
		Page<ProjectStakeholderType> items = this.projectStakeholderTypeDao.findAll(condition,pageRequest);
		if(items.getTotalElements() > 0){
			ProjectStakeholderType item = items.getContent().get(0);
			return item.getOrderNumber()+1;
		}
		return 1;
	}
	
	public boolean changeOrderNumber(Long id, Long targetId){
		ProjectStakeholderType currentModule = this.projectStakeholderTypeDao.findOne(id);
		ProjectStakeholderType targetModule = this.projectStakeholderTypeDao.findOne(targetId);
		int orderNumber = currentModule.getOrderNumber();
		currentModule.setOrderNumber(targetModule.getOrderNumber());
		targetModule.setOrderNumber(orderNumber);
		
		this.projectStakeholderTypeDao.save(currentModule);
		this.projectStakeholderTypeDao.save(targetModule);
		return true;
	}

}
		
